//
//  SYWLDateTool.m
//  tripBySoyoung
//
//  Created by wlighting on 16/7/25.
//  Copyright © 2016年 soyoung. All rights reserved.
//

#import "SYWLDateTool.h"

@implementation SYWLDateTool



/** 订单倒计时结束时间 */
+ (NSString *)getOrderEndTimeWithTimeInterval:(double)dateString andAddOrderTime:(NSString *)addString {
    NSTimeInterval dateTime = [addString doubleValue] + dateString;
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyyy-MM-dd-HH:mm:ss";
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    return [fmt stringFromDate:date];
}

+ (NSString *)getDateAccountToHHmm:(NSString *)dateString {
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat= @"HH:mm";
    
    return [fmt stringFromDate:date];
    
    
}

+ (NSString *)getDateAccountToSecond:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd HH:mm:ss";
    
    return [fmt stringFromDate:date];


}

+ (NSString *)getDateAccountToMMddHHmmSecond:(NSString *)dateString {
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM-dd  HH:mm";
    
    return [fmt stringFromDate:date];
    
    
}

+ (NSString *)getFlightNavBarTitleStyleAccountToDay:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"MM月dd日";
    
    return [fmt stringFromDate:date];
    
    
}


/** 获取低价机票开始的时间 */
+ (NSArray *)getFlightStartAndEndDateStyleWithDateStringArray:(NSString *)dateString{
    
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0 * 3600];
    NSTimeInterval delta = [zone secondsFromGMTForDate:[NSDate date]];
    NSString *string = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] + delta];
    NSString *currentDateString = [[string componentsSeparatedByString:@"."]objectAtIndex:0];
    int currentDate = [currentDateString intValue];
    
    int searchDate = [dateString intValue];
    int deltaDays = (int )(searchDate - currentDate)/(24*60*60);
    SYLog(@"searchDate==%d,currentDate ==%d,deltaDays ==%d",searchDate,currentDate,deltaDays);
    
    NSDateFormatter *fmt =[ [NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";

    
    if (deltaDays >= 15) {
//        大于15天的情况,直接计算
        int statDateMark = [dateString intValue] - 24*60*60*15;
        NSDate *startSearchDate = [NSDate dateWithTimeIntervalSince1970:statDateMark];
        NSString *statDatestring = [fmt stringFromDate:startSearchDate];
        
        int endDateMark = [dateString intValue] + 24*60*60*14;
        NSDate *endSearchDate = [NSDate dateWithTimeIntervalSince1970:endDateMark];
        NSString *endDatestring = [fmt stringFromDate:endSearchDate];
        
        return @[statDatestring,endDatestring];
        
    }else{
    
        NSDate *date = [NSDate date];
        NSString *statDatestring = [fmt stringFromDate:date];
        
        int endDateMark = [dateString intValue] + 24*60*60*14;
        NSDate *endSearchDate = [NSDate dateWithTimeIntervalSince1970:endDateMark];
        NSString *endDatestring = [fmt stringFromDate:endSearchDate];
    
        return @[statDatestring,endDatestring];
    
    
    }
    
    
    
}
/** 获取 MM-dd 格式的时间串 */
+ (NSString *)getHotelNavBarTitleStyleAccountToDay:(NSDate *)date{
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM-dd";
    
    return [fmt stringFromDate:date];
}

/** 获取 MM-dd 格式的时间串 */
+ (NSString *)getMonthAndDayeAccountToDay:(NSDate *)date {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM月dd日";
    
    return [fmt stringFromDate:date];
}

/** 获取 MM月dd日 格式的时间串 */
+ (NSString *)getMonthAndDayeAccountToDayString:(NSString *)dateString {
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM月dd日";
    
    return [fmt stringFromDate:date];
}

+ (NSString *)getMMDDEEStyleAccountToTodayTommorrow:(NSDate *)date {

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    if ([self isTodayWithCompareDate:date andDays:0]) {
        // 今天
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 今天",preDateString];
    }else if ([self isTodayWithCompareDate:date andDays:1]){
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 明天",preDateString];
    }else{
        fmt.dateFormat = @"MM-dd EEE";;
        return [fmt stringFromDate:date];
        
    }
    
}


+ (NSString *)getMMDDEEStyleAccountToDayString:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
  
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    if ([self isTodayWithCompareDate:date andDays:0]) {
        // 今天
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 今天",preDateString];
    }else if ([self isTodayWithCompareDate:date andDays:1]){
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 明天",preDateString];
    }else{
        fmt.dateFormat = @"MM-dd EEE";;
        return [fmt stringFromDate:date];
        
    }

}


+ (NSString *)getMMDDStyleAccountToDayString:(NSString *)dateString {
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"MM-dd";
    if ([self isTodayWithCompareDate:date andDays:0]) {
        return [NSString stringWithFormat:@"今天 %@",[fmt stringFromDate:date]];
    }else if ([self isTodayWithCompareDate:date andDays:1]){
        return [NSString stringWithFormat:@"明天 %@",[fmt stringFromDate:date]];
    }else{
        return [fmt stringFromDate:date];
    }
    
}


+ (NSString *)getMMDDEEStyleAccountToDay:(NSDate *)date {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    if ([self isTodayWithCompareDate:date andDays:0]) {
        // 今天
        fmt.dateFormat = @"MM月dd日";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 今天",preDateString];
    }else if ([self isTodayWithCompareDate:date andDays:1]){
        fmt.dateFormat = @"MM月dd日";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"%@ 明天",preDateString];
    }else{
        fmt.dateFormat = @"MM月dd日 EEE";
        return [fmt stringFromDate:date];
    }
    
}



+ (NSString *)getTextMMDDEEStyleAccountToDay:(NSDate *)date {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    if ([self isTodayWithCompareDate:date andDays:0]) {
        // 今天
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"今天 %@",preDateString];
    }else if ([self isTodayWithCompareDate:date andDays:1]){
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"明天 %@",preDateString];
        
        
    }else if ([self isTodayWithCompareDate:date andDays:2]){
        fmt.dateFormat = @"MM-dd";
        NSString *preDateString = [fmt stringFromDate:date];
        return [NSString stringWithFormat:@"后天 %@",preDateString];
        
    }else{
        fmt.dateFormat = @"yyyy-MM-dd";
        return [fmt stringFromDate:date];
        
    }
}


+ (BOOL)isTodayWithCompareDate:(NSDate *)compareDate andDays:(NSTimeInterval)day {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    
    fmt.dateFormat = @"yyyy-MM-dd";
    
    NSString *nowString = [fmt stringFromDate:[[NSDate date] dateByAddingTimeInterval:day*24*60*60]];
    
    NSString *compareDateString = [fmt stringFromDate:compareDate];
    
    return [nowString isEqualToString:compareDateString];
    
}


/** 从时间戳获取yyy-MM-dd格式的字符串 */
+ (NSString *)getDateAccountToDayFromDate:(NSDate *)date {

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.dateFormat = @"yyyy-MM-dd";
    return [fmt stringFromDate:date];
    
}

/** 获取低价机票结束的时间 */
//+ (NSString *)getFlightEndDateDateStyleStringArray{
//    
//    NSDate *date = [NSDate dateWithTimeIntervalSinceNow:24*60*60*30*1];
//    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
//    fmt.dateFormat= @"yyy-MM-dd";
//    return [fmt stringFromDate:date];
//}


+ (NSString *)getFlightSuitDateStyleAccountToDay:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy年MM月dd日";
    
    return [fmt stringFromDate:date];
    
    
}

/** 获取 MM-dd 格式的时间串 */
+ (NSString *)getMMDDAccountToDay:(NSString *)dateString{

    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"MM-dd";
    
    return [fmt stringFromDate:date];

}

+ (NSDate *)getNSDateAccountToDayString:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    
    return [NSDate dateWithTimeIntervalSince1970:dateTime];

}


+ (NSString *)getDateAccountToNSDate:(NSDate *)date {
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";
    
    return [fmt stringFromDate:date];
}



+ (NSString *)getDateAccountToDay:(NSString *)dateString{

    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";
    
    return [fmt stringFromDate:date];


}



+ (NSString *)getDateAndWeekAccountToDay:(NSString *)dateString{
    
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"MM-dd EEE";
    
    return [formatter stringFromDate:date];

}


/** 从时间戳获取 年 */
+ (NSString *)getYearAccountToDay:(NSString *)dateString {
    NSTimeInterval dateTime = [dateString doubleValue];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dateTime];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"MM年";
    return [formatter stringFromDate:date];

}


+ (NSInteger )getTotalDaysWihtEndDate:(NSString *)endDate andStartDate:(NSString *)startDate{


    NSTimeInterval endDateDoubleV = [endDate doubleValue];
    NSTimeInterval startDateDoubleV = [startDate doubleValue];

    NSInteger totalDays = (NSInteger)(endDateDoubleV - startDateDoubleV)/(24*60*60);

    return totalDays;

}



//获得当前若干天后的时间戳
+ (NSString *)getCurrentDateStringWithLineBefore:(int)days{
    
//    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0 * 3600];
    NSTimeInterval delta = [zone secondsFromGMTForDate:[NSDate date]];
    NSString *string = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] + delta];
    NSString *dateString = [[string componentsSeparatedByString:@"."]objectAtIndex:0];
    int beforeDate = [dateString intValue] + days *24*60*60;
    return [NSString stringWithFormat:@"%d",beforeDate];
    
}

//获得当前的时间戳
+ (NSString *)getCurrentDateString{
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0 * 3600];
    NSTimeInterval delta = [zone secondsFromGMTForDate:[NSDate date]];
    NSString *string = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] + delta];
    NSString *dateString = [[string componentsSeparatedByString:@"."]objectAtIndex:0];
    return dateString;

}

//获取当前时间戳
+ (NSString *)getCurrentTimeString {
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time = [date timeIntervalSince1970];// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeString = [NSString stringWithFormat:@"%.0f", time];
    return timeString;
}



//获得当前的时间戳
/*
+ (NSString *)getCurrentSystemDateString {
    
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeInterval delta = [zone secondsFromGMTForDate:[NSDate date]];
    NSString *string = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] + delta];
    NSString *dateString = [[string componentsSeparatedByString:@"."]objectAtIndex:0];
    return dateString;
    
}*/

/**根据格林威治时间,返回中国时间的字符串*/
+ (NSString *)getDateStringWithDate:(NSDate *)date{
    
    NSString *string = [NSString stringWithFormat:@"%f",[date timeIntervalSince1970]];
    NSString *dateString = [[string componentsSeparatedByString:@"."] objectAtIndex:0];
    return dateString;
    
}

/** 获取当前时间的yyy.MM.dd时间格式 */
+ (NSString *)getCurrentDate{

    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyy.MM.dd";
    
    return [formatter stringFromDate:[NSDate date]];

}


/** 获取当前时间的yyy-MM-dd时间格式 */
+ (NSString *)getCurrentDateWithLine{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    formatter.dateFormat = @"yyy-MM-dd";
    
    return [formatter stringFromDate:[NSDate date]];
    
}



//获取零时的时间戳
+ (NSString *)getDateStringWithOutHourMinusSecond:(NSDate *)date{

    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    
    fmt.dateFormat= @"yyy-MM-dd";
    
    NSString *tempDateString = [fmt stringFromDate:date];
    //SYLog(@"tempDateString == %@",tempDateString);
    NSDate *tempDate = [fmt dateFromString:tempDateString];
    //SYLog(@"tempDate == %@",tempDate);
    return [self getDateStringWithDate:tempDate];

}

//获取零时的时间
+ (NSDate *)clearDateWithOutHourMinusSecond:(NSDate *)date{
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    [fmt setTimeZone:localTimeZone];
    NSString *tempDateString = [fmt stringFromDate:date];
    
    return  [fmt dateFromString:tempDateString];
}

+ (NSDate *)clearDateWithOutHourMinusSecondWithLocalDate:(NSDate *)date{
    //由于系统原因.  date必须先从中国时间转换为世界时间,然后, 再进行formatter的操作.操作完毕后,再用这个世界时间生成的字符串转换成nsdate这个时候还是格林威治时间.
    //但是当再调用since1970方法的时候,返回的就是中国时间了
    NSDate *worldDate = [SYWLDateTool localDateToWorldDate:date];
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";
    [fmt setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    [fmt setTimeZone:localTimeZone];
    NSString *tempDateString = [fmt stringFromDate:worldDate];
    
    return  [fmt dateFromString:tempDateString];
}
/** 获取 MM-ddEEE 格式的时间串 */
+ (NSString *)getDateAccountToMMddEEWithFormaterDate:(NSString *)dateString{
    NSDateFormatter *fmt=[[NSDateFormatter alloc]init];
    fmt.dateFormat= @"yyy-MM-dd";
    
    NSDate *wrapDate = [fmt dateFromString:dateString];
    fmt.dateFormat= @"MM-dd EEE";
    
    return [fmt stringFromDate:wrapDate];
}


//获得三个月的时间戳
+ (NSString *)getThreeMonthsDateString{
    
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSTimeZone *zone = [NSTimeZone timeZoneForSecondsFromGMT:0 * 3600];
    NSTimeInterval delta = [zone secondsFromGMTForDate:[NSDate date]];
    NSString *string = [NSString stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970] + delta];
    NSString *dateString = [[string componentsSeparatedByString:@"."] objectAtIndex:0];
    int date = [dateString intValue] + 24*60*60*30*3;
    
    return [NSString stringWithFormat:@"%d",date];
    
}

/** 获取到达时间是多少天之后*/
+ (NSString *)getMoreDayFromStartTime:(NSString *)startTime costTime:(NSString *)costTime{
    NSString *moreDayString = @"";
    NSArray *timeArray = [startTime componentsSeparatedByString:@":"];
    NSInteger minutes4Day = 24 * 60;
    NSString *hourString = timeArray[0];
    NSString *minutesString = timeArray[1];
    NSInteger hour = hourString.integerValue;
    NSInteger minutes = minutesString.integerValue;
    NSInteger startDayTimeLeft = minutes4Day - hour * 60 - minutes;
    
    NSInteger costTimeInt = costTime.integerValue;
    
    NSInteger costTimeLeft = costTimeInt - startDayTimeLeft;//减去当日的时间后，剩余的旅行时间
    if(costTimeLeft >= 0){//旅行时间比开始日剩余的时间多，意味着要夸日才能到达
        NSInteger costTimeDay = costTimeLeft / minutes4Day;//旅行时间是多少天
        NSInteger modTime = costTimeLeft % minutes4Day;//模多少
        if(modTime != 0){
            costTimeDay += 1;
        }
        moreDayString = [NSString stringWithFormat:@"%ld",(long)costTimeDay];
    }else{
        moreDayString = @"0";
    }
    
    return moreDayString;
}
+ (BOOL)checkTrainAprartWithin48hoursWithStartTime:(NSString *)startTime {
    //2018-11-22 06:48
    NSTimeInterval time = [SYWLDateTool timeIntervalFromTodayWithStartTime:startTime];
    if(time > 0){//证明是今天以后的
        if(time < 60 * 60 * 24 * 2){//60 * 60 = 每小时秒数 * 24 * 2 = 48小时以内是不能改签目的地.
            return YES;
        }
        return NO;
    }
    return YES;
}
+ (BOOL)checkTrainCouldBeEndorse:(NSString *)startTime {
    //2018-11-22 06:48
    NSTimeInterval time = [SYWLDateTool timeIntervalFromTodayWithStartTime:startTime];
    if(time > 0){//证明是今天以后的
        if(time <  60 * 35){//每分钟60秒* 分钟数 ,35分钟以内是不能改签的了
            return NO;
        }
        return YES;
    }
    return NO;
}
+ (NSTimeInterval)timeIntervalFromTodayWithStartTime:(NSString *)startTime{
    //车次出发日期
    NSDate *startDate = [SYWLDateTool localDateWithStartTime:startTime];
    //当地当时日期
    NSDate *today = [SYWLDateTool worldDateToLocalDate:[NSDate date]];
//    NSDate *today = [NSDate date];
//    NSString *todayString = [formatter stringFromDate:today];
//    NSDate *finalToday = [formatter dateFromString:todayString];
    
    return [startDate timeIntervalSinceDate:today];
}
//本地时间的出发时间
+ (NSDate *)localDateWithStartTime:(NSString *)startTime{
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:localTimeZone];
    [formatter setDateFormat:@"yyyy-MM-ddHH:mm"];
    //车次出发日期
    NSDate *startDate = [SYWLDateTool worldDateToLocalDate:[formatter dateFromString:startTime]];
    return startDate;
}
+ (NSDate *)localDateToWorldDate:(NSDate *)localdate{
    NSTimeZone *worldTimeZone = [NSTimeZone defaultTimeZone];
    //获取世界时区
//    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    
    //计算中国时间与世界时区的时间偏差值
    NSInteger offset = [worldTimeZone secondsFromGMTForDate:localdate];
    
    //中国时间＋偏差值 得出世界区时间
    NSDate *worldDate = [localdate dateByAddingTimeInterval:-offset];
    
    return worldDate;
}
//世界时间转换为本地时间
+ (NSDate *)worldDateToLocalDate:(NSDate *)date
{
    //获取本地时区(中国时区)
    NSTimeZone* localTimeZone = [NSTimeZone localTimeZone];
    
    //计算世界时间与本地时区的时间偏差值
    NSInteger offset = [localTimeZone secondsFromGMTForDate:date];
    
    //世界时间＋偏差值 得出中国区时间
    NSDate *localDate = [date dateByAddingTimeInterval:offset];
    
    return localDate;
    
    //两种方式
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    [formatter setDateStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的日期格式
//    [formatter setTimeStyle:NSDateFormatterFullStyle];// 修改下面提到的北京时间的时间格式
//    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"];// 此行代码与上面两行作用一样，故上面两行代码失效
//
//    NSDate *datenow = [NSDate date];
//    NSLog(@"%@", datenow);// 这个时间是格林尼治时间
//    NSString *dateString = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
//    NSLog(@"%@", dateString);// 这个时间是北京时间戳
//    NSString *nowtimeStr = [formatter stringFromDate:datenow];
//    NSLog(@"%@", nowtimeStr);// 这个时间是北京时间
}
+(NSString *)timeSwitchTimestamp:(NSString *)formatTime andFormatter:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:format]; //(@"YYYY-MM-dd hh:mm:ss") ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Beijing"];
    [formatter setTimeZone:timeZone];
    NSDate* date = [formatter dateFromString:formatTime]; //------------将字符串按formatter转成nsdate
    //时间转时间戳的方法:
    NSInteger timeSp = [[NSNumber numberWithDouble:[date timeIntervalSince1970]] integerValue];
    NSLog(@"将某个时间转化成 时间戳&&&&&&&timeSp:%ld",(long)timeSp); //时间戳的值
    return [NSString stringWithFormat:@"%zd",timeSp ];
}

+ (NSString *)distanceTimeWithBeforeTime:(NSInteger)beTime
{
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    NSInteger distanceTime = now - beTime;
    NSString * distanceStr;
    
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];
    
    if (distanceTime < 60) {//小于一分钟
        distanceStr = @"刚刚";
    }
    else if (distanceTime <60*60) {//时间小于一个小时
        distanceStr = [NSString stringWithFormat:@"%ld分钟前",(long)distanceTime/60];
    }
    else if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = [NSString stringWithFormat:@"今天 %@",timeStr];
    }
    else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        if ([nowDay integerValue] - [lastDay integerValue] ==1 || ([lastDay integerValue] - [nowDay integerValue] > 10 && [nowDay integerValue] == 1)) {
            distanceStr = [NSString stringWithFormat:@"昨天 %@",timeStr];
        }
        else{
            [df setDateFormat:@"MM-dd HH:mm"];
            distanceStr = [df stringFromDate:beDate];
        }
        
    }
    else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    else{
        [df setDateFormat:@"yyyy-MM-dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];
    }
    return distanceStr;
}
@end
